"""
@author: xxw
@file: 01.高阶函数.py
@time: 2023/5/29 16:12
"""

import math
#高阶函数
# 把函数作为参数传入，这样的函数称为高阶函数

# def func1(f, a, b):
#     f(a, b)

########map
#映射
# def func(item):
#     return item*item
#
# a = [1,-2,3,-4,5]
# result = map(func, a)
# print(list(result))
# result1 = map(abs, a)
# print(list(result1))
# s1 = "abc"
# result2 = map(ord, s1)
# print(list(result2))
#
# lst1 = [1, 2, 3]
# lst2 = [4, 5, 6]
# result3 = map(lambda x,y:x*y,lst1,lst2)
# print(list(result3))
#
# float_list = [2.3452, -32.32424324]
# result4 = map(lambda x:round(x,2), float_list )
# print(list(result4))
#
# # 数据的转换
# d1 = {
#     "CMCC": "中国移动",
#     "CU": "中国联通",
#     "CT": "中国电信"
# }
#
# d2 = {"wenbi": "CU", "xxw": "CMCC", "chenli": "CT"}
# result5 = map(lambda x:(x[0],d1[x[1]]),d2.items())
# print(dict(result5))

#######filter
# 过滤
# 传递函数进去  函数的结果为真就保留，为假就删除
# lst = [1, 2, 5, 6, 8, 11]
# result = filter(lambda x: x % 2, lst)
# print(list(result))
# # 过滤1000以内，开平方是整数的数
# result2 = filter(lambda x: int(math.sqrt(x))**2 == x , range(1, 1000))
# print(list(result2))

######sorted 排序

lst1 = [3, 2, 8, 5, 9]
print(sorted(lst1))
lst2 = [-11, 2, 9, -4, 8]
print(sorted(lst2, key=abs))
lst3 = ["a","aaa", "xyzset", "sd", "sadfasdf"]
print(sorted(lst3, key=len))

lst4 = [(1,2), (3,4), (1,0), (2,3)]
print(sorted(lst4))

str1 = "aBcY114DcxP5"
# print(sorted(str1, key=lambda ))